<template>
  <div>
    <div style="margin-top: 10px;">
      <el-input v-model="msg" placeholder="请输入内容" clearable style="width: 800px;"></el-input>
      <el-button type="primary" @click="onSubmit" style="margin-left: 10px; margin-bottom: 10px;">提交</el-button>
    </div>

    <el-table :data="leaveMsg" border style="width: 100%">
      <el-table-column type="index" :index="indexMethod" label="id" />
      <el-table-column prop="createTime" label="日期" width="180" />
      <el-table-column prop="content" label="内容" />
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import {
    listAll,
    deleteOne,
    insertOne
  } from '@/api/LeaveMsg.js'

  export default {
    data() {
      return {
        leaveMsg: [],
        msg: ''
      }
    },

    mounted() {
      var that = this
      listAll().then(resp => {
        that.leaveMsg = resp.data
      }).catch(error => {
        console.log(error)
      })

    },

    methods: {
      handleDelete(index, row) {
        var that = this
        deleteOne(row.id).then(resp => {
          that.$message({
            type: "success",
            message: "删除成功"
          })
          that.leaveMsg.splice(index, 1)
        }).catch(error => {
          console.log(error)
        })

      },

      indexMethod(index) {
        return index + 1
      },

      onSubmit() {
        var that = this
        insertOne({
          content: this.msg
        }).then(resp => {
          that.$message({
            type: 'success',
            message: '提交成功'
          })
          that.leaveMsg.unshift(resp.data)
          that.msg = ''
        })
      }
    },


  }
</script>

<style>
</style>
